package com.ktjy.service.impl;

import com.ktjy.entity.Information;
import com.ktjy.entity.Page;
import com.ktjy.entity.R;
import com.ktjy.mapper.InformationMapper;
import com.ktjy.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("InformationService")
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationMapper informationMapper;
    @Override
    public Page findPageVo(Information information, Integer pageNo, Integer pageSize) {
            Page page = new Page();
            //查询总记录数
            Integer count = informationMapper.count();
            page.setRows(pageSize);
            page.setTotal(count);
            page.setTotalPage(pageNo);
            //查询信息
            List<Information> list = informationMapper.page(page.offset(), page.getRows());
            page.setList(list);
            return page;
    }

    @Override
    public int delete(Long id) {
        return informationMapper.delete(id);
    }

    @Override
    public int insert(Information information) {
        return informationMapper.add(information);
    }

    @Override
    public int updateContent(Long id) {
        return informationMapper.updateContent(id);
    }

    @Override
    public int updateReplyCount(Long id) {
        return informationMapper.updateReplyCount(id);
    }

    // 修改 selectByRep 方法，使用 selectList 而不是 selectOne
    @Override
    public List<Information> selectByRep(Long id) {
        try {
            System.out.println("开始查询ID为 " + id + " 的信息");
            
            // 查询主信息
            List<Information> list = informationMapper.selectByRep(id);
            System.out.println("查询到主信息: " + (list != null ? list.toString() : "null"));
            
            // 这里可以添加查询回复的逻辑，但需要先在 InformationMapper 中添加相应方法
            
            return list;
        } catch (Exception e) {
            System.err.println("查询出错: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
